//
//  TrainingSkillParser.m
//  iEVETracker
//
//  Created by Scott Guyer on 7/13/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "TrainingSkillParser.h"
#import "DateUtility.h"
#import "defs.h"

@implementation TrainingSkillParser


//<eveapi version="1">
//  <currentTime>2007-06-21 11:01:10</currentTime>
//  <result>
//    <trainingEndTime>6/21/2007 9:27:50 PM</trainingEndTime>
//    <trainingStartTime>6/21/2007 11:00:38 AM</trainingStartTime>
//    <trainingTypeID>3347</trainingTypeID>
//    <trainingStartSP>4000</trainingStartSP>
//    <trainingDestinationSP>22628</trainingDestinationSP>
//    <trainingToLevel>2</trainingToLevel>
//    <skillInTraining>1</skillInTraining>
//  </result>
//  <cachedUntil>2007-06-21 11:01:20</cachedUntil>
//</eveapi>

- (void) handle_trainingToLevel
{
	[_data setObject:[NSString stringWithString:_currentString] forKey:@"toLevel"];
}

- (void) handle_skillInTraining
{
	if ( [_currentString isEqualToString:@"1"] )
	{
		// Then we are training
		_training = YES;
		[_data setObject:[NSString stringWithString:@"1"] forKey:@"training"];
	}	
}

- (void) handle_trainingStartSP
{
	[_data setObject:[NSString stringWithString:_currentString] forKey:@"startSP"];
}

- (void) handle_trainingDestinationSP
{
	[_data setObject:[NSString stringWithString:_currentString] forKey:@"endSP"];
}

- (void) handle_trainingTypeID
{
	[_data setObject:[NSString stringWithString:_currentString] forKey:@"typeID"];
}

- (void) handle_trainingStartTime
{
	NSDateFormatter* df = [[NSDateFormatter alloc] init];
	[df setFormatterBehavior:NSDateFormatterBehavior10_4];
	[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
	//[df setDateFormat:@"%Y-%m-%d %H:%M:%S"];
	
	NSDate* startDate = [df dateFromString:_currentString];
	if ( startDate != nil )
		[_data setObject:startDate forKey:@"startDate"];	
	[df release];
}

- (void) handle_trainingEndTime
{
	NSDateFormatter* df = [[NSDateFormatter alloc] init];
	[df setFormatterBehavior:NSDateFormatterBehavior10_4];
	[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];	
//	[df setDateFormat:@"%Y-%m-%d %H:%M:%S"];
	NSDate* result = [df dateFromString:_currentString];
	if ( result != nil )
		[_data setObject:(NSDate*)result forKey:@"endDate"];
	[df release];
}

- (void) handle_cachedUntil
{
	NSDateFormatter* df = [[NSDateFormatter alloc] init];
	[df setFormatterBehavior:NSDateFormatterBehavior10_4];
	[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//	[df setDateFormat:@"%Y-%m-%d %H:%M:%S"];

	NSDate* cachedUntil = [df dateFromString:_currentString];
	NSDate* local = [DateUtility convertUTCToLocal:cachedUntil];
	[_data setObject:local forKey:@"cachedUntil"];
	[df release];
}



- (NSDictionary*) parseXml:(NSData*)xmlData 
{
	if ( _parser == nil )
	{
		_parser = [NSXMLParser alloc];
	}
	[_parser initWithData:xmlData];
	[_parser setDelegate:self];
	
	_data = [[NSMutableDictionary alloc] init];
	
	_training = NO;
	if ( [_parser parse] )
	{
		[_data setObject:[NSDate date] forKey:@"lastLoad"];
		return _data;
	}
	else
	{
		NSLog(@"error parsing character list xml: %@", 
			  [_parser parserError]);
		[_data release];
		return nil;
	}
}


- (void) parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
   namespaceURI:(NSString *)namespaceURI 
  qualifiedName:(NSString *)qualifiedName 
	 attributes:(NSDictionary *)attributeDict
{
	NSRange r;
	r.location = 0;
	r.length = [_currentString length];
	[_currentString deleteCharactersInRange:r];	
}


- (void) parser:(NSXMLParser *)parser 
  didEndElement:(NSString *)elementName 
   namespaceURI:(NSString *)namespaceURI 
  qualifiedName:(NSString *)qName
{
	NSString* handlerName = [NSString stringWithFormat:@"handle_%@", elementName];
	SEL handler = NSSelectorFromString( handlerName);
	
	if ( [self respondsToSelector:handler] )
	{
		[self performSelector:handler];
	}	
}


- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
	if ( _currentString == nil )
	{
		_currentString = [[NSMutableString alloc] initWithCapacity:255];
	}
	
	[_currentString appendString:string];
}




- (void) dealloc
{
	NSLog(@"%s 0x%x", __FUNCTION__, self);
	
	WIPE( _parser)
	WIPE( _data)
	WIPE( _currentString)
	
	[super dealloc];
}



@end
